<template>
  <div>
    <el-breadcrumb separator="/" class="submenu">
      <el-breadcrumb-item><a href="javascript:;">RFI动态信息</a></el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main-table">
      <el-tabs v-model="addRfi" @tab-click="handleClick">
        <el-tab-pane label="主信息" name="first">
          <rfiBase :rfiBaseForm="rfiBaseForm"></rfiBase>
        </el-tab-pane>
        <el-tab-pane label="指定反馈资料" name="second">
          <addItem @feedbackData="feedbackData" :tableData='tableData'></addItem>
        </el-tab-pane>
        <el-tab-pane label="选择供应商" name="thirdly">
          <vendors-transfer ref="trans" 
          @transAdd="addTrans" width="100%" :checkName="['id','vendorName']" 
          :title="{left:'请选择供应商',right:'已选择供应商'}"
          :searchForm="searchForm" :selectData="selectData" 
          :url="lib.config.host.system + '/system/orgmgt/organization/queryForNpcDatalist'">
          </vendors-transfer>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div style="margin-left:30px" class="el_submit" >
      <el-button type="primary" icon="el-icon-time" @click="saveOrUpdate()">保存</el-button>
      <el-button type="success" icon="el-icon-success" @click="saveAndPublish()">发布</el-button>
      <router-link to="/Index/Supplier/Rfi">
        <el-button type="info" icon="el-icon-d-arrow-left">返回</el-button>
      </router-link>
    </div>
  </div>
</template>
<script>
  import qs from 'qs';
  import RfiBase from './rfiBase.vue'
  import SelectVendors from './selectVendors.vue'
  import AddItem from './addItem.vue'
  import vendorsTransfer from '@/components/common/select-ext/vendorsTransfer.vue';
  export default {
    components: {
      RfiBase,
      SelectVendors,
      AddItem,
      vendorsTransfer
    },
    data() {
      return {
        tableData:[],
        addRfi: 'first',
        dataFeedbackList:[],
        dataVendorList:[],
        selectData:[],
        searchForm:[
          {key:'vendorType',type:'',value:''},
				  {key:'vendorCodeOrVendorName',type:'text',placeholder:'请输入名称',value:''}
        ],
        rfiBaseForm:{
          attFileUrl: '',
          attFileName: ''
        }
      };
    },
    mounted(){
      if(this.$route.query.rfiId != undefined){
        this.initData1(this.$route.query.rfiId);
      }
    },
    methods: {
      handleClick(tab, event) {
        switch (this.addRfi){
          case 'first':
            break;
          case 'second':
            break;
          case 'thirdly':
            //选择供应商后查询右侧穿梭框数据
            // this.searchTrans({vendorCodeOrVendorName:'', page:1, rows:10});
            break;
          default:
            break;
        }
      },
      searchTrans(parameter){
        //穿梭框右边数据获取
        var par = {};
        if (parameter != "") {
          par = parameter
        }
        this.axios.post(this.lib.config.host.system + "/system/orgmgt/organization/queryForNpcDatalist", qs.stringify(par)).then((res) => {
          this.selectData=res.data.rows;
        })
        .catch(function (error) {
            console.log(error);
        });
      },
      addTrans(data){
        this.dataVendorList=data
        console.log(data);

      },
      feedbackData(data){
        this.dataFeedbackList = data;
      },
      changeVendorData(data){
        this.dataVendorList = data;
      },
      saveOrUpdate(){
        let data={
          ...this.rfiBaseForm,
          rfiVendorList:this.dataVendorList,
          itemsList:this.dataFeedbackList
        }
        this.axios.post(this.lib.config.host.vendorMgt + "/vendormgt/vsp/rfi/save",data).then((res) => {
          if(res.data.success == false){
                this.$message(res.data.message);
          }else{
            this.$router.push('/Index/Supplier/Rfi');
            this.$message('保存成功');
          }
        })
        .catch(function (error) {
            console.log(error);
        });
      },
      // 保存&发布
      saveAndPublish(){
        let data={
          ...this.rfiBaseForm,
          rfiVendorList:this.dataVendorList,
          itemsList:this.dataFeedbackList
        }
          this.axios.post(this.lib.config.host.vendorMgt + "/vendormgt/vsp/rfi/saveAndPublish",data).then((res) => {
            if(res.data.success == false){
              this.$message(res.data.message);
            }else{
              this.$router.push('/Index/Supplier/Rfi');
              this.$message('发布成功');
            }
        })
        .catch(function (error) {
            console.log(error);
        });
      },
      initData1(id){
        this.axios.post(this.lib.config.host.vendorMgt + "/vendormgt/vsp/rfi/toEdit/" + id,{}).then((res) => {
            this.rfiBaseForm = res.data.rfi;
            this.selectData = res.data.rfiVendorList;
            this.tableData = res.data.vdRfiItemList;
        })
        .catch(function (error) {
            console.log(error);
        });
      }
    }
  };
</script>
